<?php
	session_start();
	include("fetchData.php");
	include("checkStudent.php");
		
	if($clean){
		if(strlen($user2)==0)
			$user2 = $user;
		
		$password = md5(uniqid(rand(), true));
		$password = sha1(substr($password, 0, 6));
		
		createAccount('s',$_POST['email'],$user2,$password);
		$que = "UPDATE account SET
				first_name = '".$_POST['fname']."',
				last_name = '".$_POST['lname']."' WHERE
				username = '$user2'";
		if(strlen($_POST['mname'])!=0){
			$que = $que . ", middle_name = '".$_POST['mname']."'";
		}
		
		$result = mysql_query($que) OR die(mysql_error());
		
		$studid_array = mysql_fetch_array(mysql_query("SELECT account_id FROM account WHERE username='$user2'"));
		$studentid = $studid_array["account_id"];
		$classid = $_GET["class_id"];
		createtake($classid, $studentid);
		
		//ini_set("SMTP", "localhost");
		//ini_set("smtp_port", "25");
		echo ini_get("SMTP") . '<br />';
		echo ini_get("smtp_port");
		mail($_POST["email"], "You've been invited to view a class record.",
		     "Your login credentials are:\n Log-in ID: $user2\n Password: " . $_POST["pwd"].
			 "From: noone@gradegrid.skytreader.net");
?>
		<head><title>GradeGrid - Student Addition Success</title></head>
		<body onload="window.resizeTo(620,400)">
			<div style="position:absolute; top:-60px; margin-top:50%; left:-290px; margin-left:50%;">
				<h3 align="center" style="margin-bottom:0px">The student had been added successfully with the following credentials:<br/></h3>
                <h3 align="center" style="margin-top:0px; margin-bottom:0px; color:#3C0">Username: <?php echo $user2;?><br/></h3>
                <h3 align="center" style="margin-top:0px">What would you like to do?</h3>
				<div align="center">
					<a href="addStudent.php?class_id=<?php echo $_GET['class_id'];?>">Add a new student</a>
					 OR 
					<a id="close" onClick="window.close()" style="text-decoration:underline; color:#03F" onMouseOver="document.getElementById('close').style.cursor='pointer'">Close this window</a>
				</div>
			</div>
			<div style="font-size:9px; position:absolute; bottom:10px; left:-40px; margin-left:50%;" align="center">GradeGrid Copyright</div>
		</body>
<?php
	} else {
		echo $error;
		echo "<br/><a id=\"close\" onClick=\"history.back()\" style=\"text-decoration:underline; color:#03F\" onMouseOver=\"document.getElementById('close').style.cursor='pointer'\">Click here to return to your form</a></div>";
	}
?>